<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{:lang('设置套餐')}</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all" />
    <link rel="stylesheet" href="__CSS__/admin.css" media="all">
</head>
<style>
    .addPackage{
        float: right;
        margin-top: -4px;
    }
    .fastSpec{
        margin-top: 4px;
    }
    .fastSpec:first-child{
        margin-left: 10px;
    }
</style>
<body>
<div class="layui-container" style="padding: 15px;">
    <div class="layui-row">
        <div class="layui-col-md12">
            <blockquote class="layui-elem-quote">
                <span>设置产品套餐</span>
                <span style="margin-left: 20px;font-size: 12px;color:orangered">【特殊套餐不参与最低价统计显示】</span>
                <button class="layui-btn layui-btn-sm addPackage" id="addPackage">添加套餐</button>
            </blockquote>
            <!--套餐表单-->
            <form class="layui-form" id="packageForm" style="padding: 8px;border: 1px #eee solid;">
                <input type="hidden" id="product_id" value="{$id}">
                <div id="packageBody" >
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-inline">
                        <button class="layui-btn" lay-submit lay-filter="admin">{:lang('提交')}</button>
                        <button type="reset" class="layui-btn layui-btn-primary">{:lang('重置')}</button>
                    </div>
                </div>

            </form>
        </div>
    </div>

</div>
</body>
<script src="__PUBLIC__/layui/layui.js"></script>
<script src="__PUBLIC__/jquery/jquery.min.js"></script>
<script>
    var form = null;
    layui.use(['layer', 'form'], function() {
        var layer = layui.layer,
            $ = layui.jquery;
            form = layui.form;

        //提交表单
        form.on('submit(admin)', function(data) {
            var id = $("#product_id").val(); //产品ID
            var data = [];
            var packageChild = $(".packageChild");
            var errMsg = '';
            for(var i = 0; i < packageChild.length; i++) {
                var datas = {};
                var datal = [];

                var specsChild = $(packageChild[i]).find(".specsChild tr");
                $specialArr = [];
                for(var k = 0; k < specsChild.length; k++) {
                    //禁止所有的规格都设置特殊套餐
                    var datass = {};
                    var specs_id = $(specsChild[k]).find("input[name=specs_id]").val();
                    if(specs_id) {
                        datass.specs_id = specs_id;
                    }
                    datass.specs_name = $(specsChild[k]).find("input[name=specs_name]").val();
                    if(datass.specs_name==''){errMsg='请补充所有的规格名！';layer.msg(errMsg);return false;}
                    datass.specs_num = $(specsChild[k]).find("input[name=specs_num]").val();
                    if(datass.specs_num==''){errMsg='请补充所有的库存！';layer.msg(errMsg);return false;}
                    datass.minorder_num = $(specsChild[k]).find("input[name=minorder_num]").val();
                    if(datass.minorder_num==''){errMsg='请补充所有的最小下单数！';layer.msg(errMsg);return false;}
                    datass.inc_order_num = $(specsChild[k]).find("input[name=inc_order_num]").val();
                    if(datass.inc_order_num==''){errMsg='请补充所有的最小增长数！';layer.msg(errMsg);return false;}
                    var specs_special = $(specsChild[k]).find("input[name=specs_special]:checked").val();
                    specs_special = (specs_special==undefined||specs_special==null) ? 0 : 1;
                    $specialArr.push(specs_special)
                    datass.specs_special = specs_special;
                    datal.push(datass);
                }
                if($.inArray(0,$specialArr) < 0){
                    errMsg='同一套餐下规格不能全部设置为特殊套餐！';layer.msg(errMsg);return false;
                }
                datas.package_name = $(packageChild[i]).find("input[name=package_name]").val();
                if(datas.package_name==''){errMsg='请补充所有的套餐名！';layer.msg(errMsg);return false;}
                var package_id = $(packageChild[i]).find("input[name=package_id]").val();
                if(package_id) {
                    datas.package_id = package_id;
                }
                datas.specsdata = datal;

                data.push(datas);
                console.log(data);
            }
            if(data.length<1){layer.msg('请先添加套餐内容');return false;}
            $.ajax({
                url: "{:url('admin/product/setBasePackage')}",
                data: {
                    id: id,
                    data: data
                },
                type: 'post',
                async: false,
                success: function(res) {
                    layer.msg(res.msg);
                    if(res.code == 1) {
                        setTimeout(function() {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        }, 1500)
                    }
                }
            });
            return false;
        })
    });
</script>

<script>
    $("#addPackage").on("click", function() {
        addPackage();
    });
    //删除套餐
    $(document).on("click", ".delPackage", function() {
        var that = this;
        var id = $(that).attr("data-id");
        if(id=='' || id==undefined){
            var child = $(that).parent(".packageChild");
            child.remove();
        }else{
            layer.confirm('删除已有套餐会删除相关的价格配置<br>是否确认删除？',function(){
                $.post("/admin/product/delBasePackage",{id:id},function(data){
                    if(data.code==200){
                        var child = $(that).parent();
                        child.remove();
                        layer.msg("{:lang('删除成功')}");
                    }else{
                        layer.msg("{:lang('删除失败')}");
                    }
                },'json')
            })
        }
    });
    //删除规格
    $(document).on("click", ".delSpecs", function() {
        var that = this;
        var id = $(that).attr("data-id");
        if(id=='' || id==undefined){
            var child = $(that).parent().parent();
            child.remove();
        }else{
            layer.confirm('删除已有规格会删除相关的价格配置<br>是否确认删除？',function(){
                $.post("/admin/product/delBaseSpecs",{id:id},function(data){
                    if(data.code==200){
                        var child = $(that).parent().parent();
                        child.remove();
                        layer.msg("{:lang('删除成功')}");
                    }else{
                        layer.msg("{:lang('删除失败')}");
                    }
                },'json')
            })
        }
    });
    //添加规格
    $(document).on("click", ".addSpecs", function() {
        var html = addSpecs();
        $(this).siblings('.childs').find('.specsChild').append(html);
        form.render();
    })
    //快速通道添加规格
    $(document).on("click", ".fastSpec", function() {
        var spec_data = $(this).attr('data-spec');
        spec_data = spec_data.split('-');
        var specs_name = spec_data[0] || '';
        var specs_num = spec_data[1] || '';
        var minorder_num = spec_data[2] || '';
        var inc_order_num = spec_data[3] || '';
        var specs_special = spec_data[4] || '';

        var html = addSpecs('',specs_name,specs_num,minorder_num,inc_order_num,specs_special);
        $(this).parent().parent().next().find('.specsChild').append(html);
        form.render();
    })
    //添加规格调用方法
    function addSpecs(specs_id,specs_name,specs_num,minorder_num,inc_order_num,specs_special)
    {
        specs_id = specs_id || '';
        specs_name = specs_name || '';
        specs_num = specs_num || 99;
        minorder_num = minorder_num || 1;
        inc_order_num = inc_order_num || 1;
        specs_special = specs_special || 0;
        var specs_special_checked = '';
        if(specs_special > 0) specs_special_checked='checked';

        var html = '' +
            ' <tr>\n' +
            '   <input type="hidden" name="specs_id" value="'+specs_id+'">\n' +
            '   <td><input name="specs_name" value="'+specs_name+'" placeholder="{:lang($table_name[0])}" autocomplete="off" class="layui-input" type="text" lay-key="2"></td>\n' +
            '   <td><input name="specs_num" value="'+specs_num+'" placeholder="{:lang($table_name[1])}" autocomplete="off" class="layui-input layui-col-xs5" type="number" lay-key="2"></td>\n' +
            '   <td><input name="minorder_num" value="'+minorder_num+'" placeholder="{:lang($table_name[2])}" autocomplete="off" class="layui-input layui-col-xs5" type="number" lay-key="2"></td>\n' +
            '   <td><input name="inc_order_num" value="'+inc_order_num+'" placeholder="{:lang($table_name[3])}" autocomplete="off" class="layui-input layui-col-xs5" type="number" lay-key="2"></td>\n' +
            '   <td><input name="specs_special" value="1" autocomplete="off" class="layui-input layui-col-xs5" type="checkbox" title="是" '+specs_special_checked+'></td>\n' +
            '   <td><span class="layui-btn layui-btn-danger layui-btn-sm delSpecs" data-id="'+specs_id+'" style="margin-top: 4px"><i class="layui-icon"></i></span></td>\n' +
            '</tr>';
        return html;
    }
    //添加套餐调用方法
    function addPackage(package_id,package_name){
        package_id = package_id || '';
        package_name = package_name || '';
        var html = '    <div class="layui-form-item packageChild" >\n' +
            '      		<label class="layui-form-label">套餐名称</label>\n' +
            '      		<div class="layui-input-inline" style="width: 500px">\n' +
            '        		<div class="layui-input-inline" style="width: 400px;padding-bottom: 15px">\n' +
            '          			<input name="package_name" value="'+package_name+'"  placeholder="套餐名称" autocomplete="off" class="layui-input" type="text" lay-key="1">\n' +
            '          			<input type="hidden" name="package_id" value="'+package_id+'">\n' +
            '        		</div>\n' +
            '          </div>\n' +

            '      		<span  class="layui-btn layui-btn-danger layui-btn-sm delPackage" data-id="'+package_id+'" style="margin-top: 4px;margin-left: 4px;float: right">删除套餐</span>\n' +
            '      		<span  class="layui-btn  layui-btn-sm addSpecs" style="margin-top: 4px;float: right">添加规格</span>\n' +
            '<div class="layui-form-item">' +
            '<label class="layui-form-label">快速通道</label>' +
            '<div class="layui-input-inline" style="width: 80%;">' +
            '<a class="layui-btn layui-btn-primary fastSpec adultSpec" data-spec="成人-99-1-1">+ 成人-99-1-1</a>' +
            '<a class="layui-btn layui-btn-primary fastSpec childrenSpec" data-spec="儿童-99-1-1">+ 儿童-99-1-1</a>' +
            '</div>' +
            '</div>'+
            '<div class="layui-form-item childs">'+
            '<label class="layui-form-label">&nbsp;</label>'+
            '<div class="layui-input-inline" style="width: 650px">'+
            '<table class="layui-table">'+
            '<thead><tr><th>{:lang($table_name[0])}</th><th>{:lang($table_name[1])}</th><th>{:lang($table_name[2])}</th><th>{:lang($table_name[3])}</th><th>{:lang("特殊套餐")}</th><th>{:lang("操作")}</th></tr></thead>'+
            '<tbody class="specsChild"></tbody>'+
            '</table>'+
            '</div>'+
            '</div>'+
            '</div>';
        $("#packageBody").append(html);
        return $("#packageBody .specsChild:last");
    }
    //頁面初始化
    var packageList = {$package|json_encode};
    if(packageList.length > 0){
        $.each(packageList,function (i,v) {
            if(v.name!==''){
                var specsChild = addPackage(v.id,v.name);
                if(v.specs.length>0){
                    $.each(v.specs,function (ii,vv) {
                        var spec_html= addSpecs(vv.id,vv.name,vv.num,vv.minorder_num,vv.inc_order_num,vv.special);
                        $(specsChild).append(spec_html);
                    })
                }
            }
        })
    }
</script>
</html>